/**
 * @author 艾尼亚尔
 * @date 2025/5/19
 * @description 一些处理函数
 */


import type {AxiosRequestConfig} from 'axios';
import type {AxiosTransform} from "@/utils/http/transformAxios.ts";

const toString = Object.prototype.toString;

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';

export interface CreateAxiosOptions extends AxiosRequestConfig {
    authenticationScheme?: string;
    transformAxios?: AxiosTransform;
}

export interface Result<T = any> {
    code: number;
    status: number;
    message: string;
    data?: T;
}

export const isFunction = <T = Function>(val: unknown): val is T => {
    return toString.call(val) === `[object Function]`;
}

export const isUrl = (url: string) => {
    return /^(http|https):\/\//g.test(url);
}

export const isString = (val: unknown): val is string => {
    return toString.call(val) === `[object String]`;
}

export const isObject = (val: any): val is Record<any, any> => {
    return val !== null && is(val, "Object");
};

export const joinTimestamp = (join: boolean, restful = false): string | object => {
    if (!join) {
        return restful ? '' : {};
    }
    const now = new Date().getTime();
    if (restful) {
        return `?_t=${now}`;
    }
    return {_t: now};
}
/**
 * @description: Format request parameter time
 */
export const formatRequestDate = (params: Recordable) => {
    if (Object.prototype.toString.call(params) !== '[object Object]') {
        return;
    }

    for (const key in params) {
        if (params[key] && params[key]._isAMomentObject) {
            params[key] = params[key].format(DATE_TIME_FORMAT);
        }
        if (isString(key)) {
            const value = params[key];
            if (value) {
                try {
                    params[key] = isString(value) ? value.trim() : value;
                } catch (error) {
                    throw new Error(error as any);
                }
            }
        }
        if (isObject(params[key])) {
            formatRequestDate(params[key]);
        }
    }
}

